﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.HtmlControls;

namespace NJL.Web.Shop
{
    public partial class ShopActivity : BasePageVisitor
    {
        NJL.BLL.NJL_COMPANY_SERVICE bll = new BLL.NJL_COMPANY_SERVICE();
        protected NJL.Model.NJL_COMPANY_SERVICE model;
        NJL.BLL.NJL_COMPANY bllCompany = new BLL.NJL_COMPANY();
        protected NJL.Model.NJL_COMPANY modelCompany;
        NJL.BLL.NJL_ACTIVE_COMMENT bllComment = new BLL.NJL_ACTIVE_COMMENT();
        NJL.Model.NJL_ACTIVE_COMMENT modelComment;
        NJL.BLL.NJL_USER bllUser = new BLL.NJL_USER();
        NJL.Model.NJL_USER modelUser = new Model.NJL_USER();
        NJL.BLL.NJL_PERSON bllPerson = new BLL.NJL_PERSON();
        NJL.Model.NJL_PERSON modelPerson;
        NJL.BLL.NJL_ACTIVE bllActive = new BLL.NJL_ACTIVE();
        NJL.Model.NJL_ACTIVE modelActive;
        protected void Page_Load(object sender, EventArgs e)
        {
            modelCompany = bllCompany.GetModel(Request.QueryString["id"].ToString());
            if (!IsPostBack)
            {
                bind();
            }
        }
        void bind()
        {
            bllActive.updateHitandDate(Request.QueryString["aid"]);
            modelActive = bllActive.GetModel(Request.QueryString["aid"]);
            litName.Text = modelActive.ACTIVE_NAME;
            litContent.Text = modelActive.ACTIVE_CONTENT;
            litCreatDate.Text = modelActive.CREATE_DATE_TIME.ToString();
            litHit.Text = modelActive.VISIT_COUNT.ToString();
       
            if (modelActive.IMAGE_CODE != "")
            {
                Image1.ImageUrl = GetImageUrl(modelActive.IMAGE_CODE, "5");
            }
            else
            {
                Image1.Visible = false;
            }
            litCompanyName.Text = modelCompany.COMPANY_NAME;
           
            LoadPageSEO();
            #region 评论
            DataSet ds = bllComment.GetList("NJL_ACTIVE_ID='" + Request.QueryString["aid"] + "'");
            rptCommentList.DataSource = ds;
            rptCommentList.DataBind();
            #endregion
        }
        /// <summary>
        /// 加载页面SEO
        /// </summary>
        private void LoadPageSEO()
        {
            Page.Title = modelActive.SEO_TITLE;
            HtmlMeta myMeta = new HtmlMeta();
            myMeta.Name = "Keywords";
            myMeta.Content = modelActive.SEO_KEYWORDS;
            this.Header.Controls.Add(myMeta);
            HtmlMeta myMeta1 = new HtmlMeta();
            myMeta1.Name = "Description";
            myMeta1.Content = modelActive.SEO_DESCRIPTION;
            this.Header.Controls.Add(myMeta1);
        }

        protected void btnComment_Click(object sender, EventArgs e)
        {
            if (UserId == "" || UserType == "-1")
            {
                Royalsoft.Common.MessageBox.ShowAndRedirect(this, "请登录后再发表", "/Login.aspx?url=" + Server.UrlEncode(Request.RawUrl));
                return;
            }
            if (Session["CheckCode"] != null)
            {
                if (txtValidate.Text.Trim().ToLower() != Session["CheckCode"].ToString().ToLower())
                {
                    Royalsoft.Common.MessageBox.Show(this, "请输入正确的验证码");
                    return;
                }
                modelComment = new Model.NJL_ACTIVE_COMMENT();
                modelComment.ID = DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random().Next(100, 999).ToString();
                modelComment.COMMENT_CONTENT = txtComment.Text.Trim();
                modelComment.CREATE_USER_ID = UserId;
                modelComment.CREATE_USER_NAME = UserName;
                modelComment.CREATE_DATE_TIME = DateTime.Now;
                modelComment.NJL_ACTIVE_ID = Request.QueryString["aid"];
                bllComment.Add(modelComment);
                string message;
                if (HandleIntegral(UserId, "3", out message))
                {

                }
                bllActive.updateCommentHitAndDate(Request.QueryString["aid"]);
                Royalsoft.Common.MessageBox.ShowAndRedirect(this, "评论成功+2分", Request.RawUrl);
            }
            else
            {
                Royalsoft.Common.MessageBox.ShowAndRedirect(this, "连接系统超时", "" + Request.RawUrl);
            }
        }

        protected void rptCommentList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView grv = (DataRowView)e.Item.DataItem;
                string userId = grv["CREATE_USER_ID"].ToString();
                Image imgUserHead = e.Item.FindControl("imgUserHead") as Image;
                modelPerson = bllPerson.GetModelByUserId(userId);
                if (modelPerson != null)
                {
                    if (modelPerson.IMAGE_CODE != "")
                    {
                        imgUserHead.ImageUrl = GetImageUrl(modelPerson.IMAGE_CODE, "U1");
                        imgUserHead.BorderWidth = 1;
                    }
                    else
                    {
                        imgUserHead.ImageUrl = "/Images/avatar.jpg";
                        imgUserHead.BorderWidth = 1;
                    }
                }
        
                LinkButton lbtn = e.Item.FindControl("lbtnDel") as LinkButton;
                if (UserId != "" && UserId.Equals(modelCompany.NJL_USER_ID))
                {
                    lbtn.Visible = true;
                }
                else
                {
                    lbtn.Visible = false;
                }
            }
        }

        protected void rptCommentList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Del")
            {
                string id = e.CommandArgument.ToString();
                if (bllComment.Delete(id))
                {
                    bind();
                }
                else
                {
                    Royalsoft.Common.MessageBox.Show(this, "删除失败");
                }
            }
        }
    }
}